/**
 * 文档列表模块，提供列表功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "msgServ","ajaxServ","datagridServ","sysmanMain","btnServ","bizProto"], 
	function($,msgServ,ajaxServ,datagridServ,sysmanMain,btnServ,bizProto) {

	'use strict';

	var    $typedg,$typegrid,$detaildg,$detailgrid,$edit_window,
			$searchbox,$searchMenu,
			_defaultConfig,listTd,toolbar,
			$panel,_bizProto,_moduleConfig,_buttonConfig,			
			docdetailEditUrl,
			doctypeQryUrl='/doctype/getTree.action',
			docdetailQryUrl='/document/query/getTypeListDomain.action',
			docdetailSaveUrl='/docdetail/save.action',
			docdetailDelUrl='/docdetail/del.action'
		   	
    
	var	private_method = {
		init : function(_options) {
			
			_moduleConfig = sysmanMain.getCurrentModuConfig();
			
			if (window.console) {
				console.info(_moduleConfig.fullBizcode +' modules gets ready and been loaded.');
			}
			
			_defaultConfig ={
				doc:null,
				rootId:null,				
				panelSelector:'div.well',
				subSystemCode:'sysman',
				moduleCode:'docdetail',
				btnSelector:'td.list-btn',
				doccode:null,
				edit_window:null,
				onWindowClose:null, //window close event
				initialized:false ,
				buttonExtends:null,//按钮方法扩展
				editEvent:null//初始化标志
			}
					
			_defaultConfig = $.extend({},_defaultConfig,_moduleConfig,_options)	
			
			if(!_defaultConfig.moduleCode){
				_defaultConfig.moduleCode = _defaultConfig.bizcode
			}
			_bizProto = new bizProto(_defaultConfig);	
			
			_bizProto.gridHeight -= 15
			
			_defaultConfig.bizProto = _bizProto
			if(_defaultConfig.editWindow){
				$edit_window = $('#' + _defaultConfig.editWindow);			
			}
			
				
			_buttonConfig={
				"新增":private_method.add,
			    "编辑":private_method.edit,
				"删除":private_method.del,
				"执行查询":private_method.doSearch								
			}
			//扩展按钮方法
			if(_defaultConfig.buttonExtends){
				var _extends = _defaultConfig.buttonExtends()
				if(_extends)
				$.extend(_buttonConfig,_extends)
			}
			
			_defaultConfig.initialized = true;
			sysmanMain.setCurrentModuConfig(_defaultConfig);
			
			private_method.initElement()
		},
		
		doSearch:function(e){
			
			var _param={}
			//获取当前模块配置
			_moduleConfig = sysmanMain.getCurrentModuConfig();	
			
			if(_moduleConfig && _moduleConfig.rootId)
				$detailgrid= $('#'+_moduleConfig.rootId +" table.dg-detail");	
			
				
			
		 	
		 	
			if($detailgrid.length>0){
				_param = private_method.getSearchValue();
				
				//增加分类参数
				if(_moduleConfig.typeid){ 
			 		_param[_moduleConfig.typeField]=_moduleConfig.typeid
			 	}
				if(_param && !$.isEmptyObject(_param)){
									
					$detailgrid.datagrid('load', {condition:_param});
//					$dg.datagrid('load', _queryParam);
				}else{
//					$dg.datagrid('load');
				}				
			}
			
			if(e &&　e.currentTarget){
				$(e.currentTarget).linkbutton('enable');
			}
		},
		getCurrentPageEl:function(){
			var _config = sysmanMain.getCurrentModuConfig();
			var tab = sysmanMain.getCurrentTab();
			//按照id查找
			if(_config.rootId){
				var doc = $(tab).find('#'+_config.rootId)
				
			
				listTd = $(doc).find(_defaultConfig.btnSelector)
				
				toolbar = $(doc).find('div.tb-list');
				$typedg = $(doc).find("table.dg-type");
				$typegrid = $typedg;
				
				$detaildg = $(doc).find("table.dg-detail");
				$detailgrid = $detaildg;
				
				$searchbox = $(doc).find("input.searchbox-detail")
				
				$searchMenu = $(doc).find("div.search-detail")
				_bizProto.pageDoc = doc
			}			
			
			
		},
		
		initElement: function () {
			_moduleConfig = sysmanMain.getCurrentModuConfig();	
			private_method.getCurrentPageEl();
			//初始化操作按钮		
			var _param = {
				bizcode:_moduleConfig.fullBizcode,
				rootPath:_bizProto.rootPath,
				container:listTd,
				callback:private_method.bindBtnEvent}
			btnServ.generateListBtn(_param);
			
			var typeQryUrl
			if(_moduleConfig.typeQryUrl){
				typeQryUrl = '/'+_moduleConfig.typeCode +'/query/'+_moduleConfig.typeQryUrl+'.action';
			}else{
				typeQryUrl='/'+_moduleConfig.typeCode +
					'/query/getTreeList.action'
			}
			
			var typeParam = {
				height : _bizProto.gridHeight,
				url : _bizProto.rootPath + typeQryUrl,
				onClickRow:private_method.getDetail
			}
//			if(_defaultConfig.doccode){
//				typeParam.queryParams={doccode:_defaultConfig.doccode}
//			}
			// 搜索框
			//$searchbox=$(doc).find("searchbox-detail"),
			if($searchbox.length>0){
				
				var _searchParam = {
					prompt : '模糊查询',
					searcher : function(value, name) {
						var obj = {};
						obj.searchName=name;
						obj.searchValue=value;							
//						private_method.doSearch(obj)
				}}
				
				//判断search菜单是否存在				
				if($searchMenu.length>0){
					_searchParam.menu=$searchMenu
				}
				$($searchbox).searchbox(_searchParam);
			}
			
			if(_moduleConfig.addition_param){
				var _code = $.parseJSON(_moduleConfig.addition_param)
				if(_code.typeid){
					typeParam.queryParams={doccode:_code.typeid}
				}				
			}
			$typegrid=$($typedg).treegrid(typeParam);
			
			
			var detailQryUrl
			if(_moduleConfig.detailQryUrl){
				detailQryUrl = _moduleConfig.detailQryUrl
			}else{
				detailQryUrl='/'+_moduleConfig.moduleCode +
					'/query/getListDomain.action'
			}
			
			
			$detailgrid=$($detaildg).datagrid({
				height :_bizProto.gridHeight,
				url : _bizProto.rootPath + detailQryUrl,
				toolbar:$(toolbar)
			});
			//关闭加载条
			$.messager.progress('close');
		},		
		
		 getDetail:function (row){
		 	var _param = {}
		 	_moduleConfig = sysmanMain.getCurrentModuConfig();
		 	_moduleConfig.typeid = row.id
		 	sysmanMain.setCurrentModuConfig(_moduleConfig);
		 	_param[_moduleConfig.typeField]=row.id
		 	$detailgrid.datagrid('load',{condition:_param})
		},
		//绑定按钮事件
		bindBtnEvent:function(){
			var _moduleConfig = sysmanMain.getCurrentModuConfig();
			
			// 绑定列表按钮事件
			var btn = $(_moduleConfig.bizProto.pageDoc).find(_moduleConfig.btnSelector +' a.function-btn')
			$(btn).unbind('click');
			$(btn).bind('click', private_method.function_btn_listenner)
			
			// 绑定列表按钮事件
			btn = $(_moduleConfig.bizProto.pageDoc).find('.search-btn .function-btn')
			$(btn).unbind('click');
			$(btn).bind('click', private_method.function_btn_listenner)
		},
		// 功能按钮监听函数
		function_btn_listenner : function(e) {

			btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})

		},
		
		getSearchValue:function(){
			
			private_method.getCurrentPageEl();
			var ret={};
			
			if($searchbox.length>0){
				var _searchName, _searchValue
				//取值
				_searchValue = $($searchbox).searchbox('getValue')
				if(_searchValue){
					_searchValue = $.trim(_searchValue)
					if(_searchValue==='')
						return
				}			
					
				//取名称
				var _searchName = $($searchbox).searchbox('getName')
				if(_searchName){
					_searchName =$.trim(_searchName)
					if(_searchName===''){
						return
					}															
				}
				
				ret[_searchName]=_searchValue;
				
				return ret
			}
				
			
		},
		// 新增函数
		add : function(e) {
			
			var _moduleConfig = sysmanMain.getCurrentModuConfig();					
			if (!_moduleConfig.typeid) {					
				msgServ.Msgslide('请选择类型');
				return
			}
			$($detailgrid).datagrid('clearSelections')
			private_method.editExecute(false);
		},
		// 编辑,flag 为form按钮可见性标志
		edit : function(e) {	
			private_method.editExecute(true);				
		},
		editExecute : function(flag) {	
			var _moduleConfig = sysmanMain.getCurrentModuConfig();
			private_method.getCurrentPageEl();
			var _param = {grid:$detailgrid,window:$edit_window,flag:flag}
			if(_moduleConfig.onWindowClose){
				_param.onClose=_moduleConfig.onWindowClose
			}
			
			_param.typeid = _moduleConfig.typeid
			_moduleConfig.editEvent(_param)
								
		},
		
		// 删除
		del : function() {
			private_method.getCurrentPageEl();
			var _moduleConfig = sysmanMain.getCurrentModuConfig();
			if (window.console) {
			console.info(_moduleConfig.fullBizcode +' delete on air ...');
			}
			
			var _p = {target:$detailgrid}
			if(_moduleConfig.moduleCode){
				_p.url = _moduleConfig.bizProto.rootPath +'/'+ _moduleConfig.moduleCode +'/del.action';
			}else{
				_p.url = _moduleConfig.bizProto.rootPath + docdetailDelUrl
			}
			datagridServ.delRow(_p);
		}
	}
	
	var public_method = {
		/**
		 * @param _options.doc	页面根节点
		 * @param _options.docroot	当前业务页面根节点
		 */
		init:function(_options){
			
			private_method.init(_options)
		}
	}	
	return public_method;
});